package t327.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import t327.mapper.PlateNumberScanmapper;
import t327.pojo.PlateNumberRecord;
import t327.service.iter.IPlateNumberScanService;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class plateNumberScanImpl implements IPlateNumberScanService {

    @Autowired
    private PlateNumberScanmapper plateNumberScanmapper;

    @Override
    public List<PlateNumberRecord> getqueryPlateNumberRecord(String plateNumber) {
        List<PlateNumberRecord> plateNumberRecord = plateNumberScanmapper.getqueryPlateNumberRecord(plateNumber);


        return plateNumberScanmapper.getqueryPlateNumberRecord(plateNumber);
    }

    public void processPayment(String plateNumber) {
        List<PlateNumberRecord> plateNumberRecords = getqueryPlateNumberRecord(plateNumber);

        if (!plateNumberRecords.isEmpty()) {
            for (PlateNumberRecord record : plateNumberRecords) {
                double fee = calculateFee(record.getStoppingTime(), record.getStoppingTime());

                deductFee(record, fee);
            }
        } else {
            // 车牌号不存在，可以进行相应的处理
        }
    }

    private void deductFee(PlateNumberRecord record, double fee) {

    }

    public double calculateFee(Date startTime, Date endTime) {
        // 获取停车时长（单位：分钟）
        long parkingDurationMinutes = (endTime.getTime() - startTime.getTime()) / (1000 * 60);

        // 超过30分钟不足1小时的按1小时计算
        if (parkingDurationMinutes >= 30 && parkingDurationMinutes < 60) {
            parkingDurationMinutes = 60;
        }

        // 计算白天时间段（7:00 - 20:00）和夜间时间段（20:00 - 次日7:00）的起始和结束时间
        Calendar dayStart = Calendar.getInstance();
        dayStart.setTime(startTime);
        dayStart.set(Calendar.HOUR_OF_DAY, 7);
        dayStart.set(Calendar.MINUTE, 0);
        dayStart.set(Calendar.SECOND, 0);

        Calendar dayEnd = Calendar.getInstance();
        dayEnd.setTime(startTime);
        dayEnd.set(Calendar.HOUR_OF_DAY, 20);
        dayEnd.set(Calendar.MINUTE, 0);
        dayEnd.set(Calendar.SECOND, 0);

        Calendar nightStart = Calendar.getInstance();
        nightStart.setTime(dayEnd.getTime());
        nightStart.add(Calendar.SECOND, 1);

        Calendar nightEnd = Calendar.getInstance();
        nightEnd.setTime(dayStart.getTime());
        nightEnd.add(Calendar.DAY_OF_MONTH, 1);

        // 初始化费用和计算系数
        double fee = 0.0;
        double dayRate = 3.0; // 白天计费系数：每增加1小时加收3元
        double nightRate = 1.5; // 夜间计费系数：每增加1小时加收1.5元

        // 遍历停车时长的每一分钟，根据时间段计算费用
        Calendar current = Calendar.getInstance();
        current.setTime(startTime);
        while (current.before(endTime)) {
            if (current.after(dayStart) && current.before(dayEnd)) {
                fee += dayRate;
            } else if (current.after(nightStart) || current.before(nightEnd)) {
                fee += nightRate;
            }
            current.add(Calendar.MINUTE, 1);
        }

        // 全天最高限额为20元
        if (fee > 20.0) {
            fee = 20.0;
        }

        return fee;
    }




    @Override
    public boolean addPlateNumberRecord(PlateNumberRecord plateNumberRecord) {

        boolean isExist = plateNumberScanmapper.addPlateNumberRecord(plateNumberRecord);

        if (!isExist) {

            // 执行添加操作，将车辆信息添加到数据库
            boolean success = plateNumberScanmapper.addPlateNumberRecord(plateNumberRecord);

            return success; // 返回添加结果
        } else {
            // 参数中有存在于数据库中的数据，不执行添加操作
            return false;
        }

//        return plateNumberScanmapper.addPlateNumberRecord(plateNumberRecord);

    }

        }